package org.zfes.snowier.zbss.xzy;

import javax.validation.constraints.NotNull;

import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.core.util.encypt.AESUtil;
import org.zfes.snowier.core.util.encypt.RSAUtil;
import org.zfes.snowier.zbss.xzy.vedio.LiveParamDto;

import com.alibaba.fastjson.JSON;

public class Test2 {
//	static String secretId="123456789";
	static String deviceNum="FF_505152069029";
	static String channelId="0001";
	static String liveId="live123456";

//	appId=0001
//			&deviceNum=FF_505152069029
//			&endata=44ab93c7096b8d490dd2cfa54e1f9bb4e550130b66e83f0a6ff43a6dc3b8bb8c6871d379e522a80167fd7570bc2c66e7f4b4a49c4d561c7a8a655bdd25267e91411c5d95d2fec86e522a26e70314943e3eed7f22e77f056ae1bcfda48ae92749
//			&secretId=123456789
//			&t=1611925892069
//			&us=0584aba285f24df3b0e20aaf9d969554
	static String appId="0001";
	
	static String secretId="123456789";
	
	static String us="0584aba285f24df3b0e20aaf9d969554";
	static String t="1611925892069";
	static String privateKey="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAI4rQFmAbxCIJCXXl4EYxaO5TKiATf5rchfGxQTYuMzVi78/J1TyL+h+yMXPnd9aX489E0z1Qe9HKqes0Ptn3TTILXNKFiDLURg01QsTrbGclsF18ua3T8Pn5MjyaTLx53rrIvlxujxa4YyuqIoatLqSd4ipT9I1D98YdH+NEDZBAgMBAAECgYAOT5JBsCj97AZffM02ZIz4hx2NVKUFOEGa3RpV+47Wp6RSkwPdjCeAasrBBr4ojotR+t7nf0Ss1mgXytCajgD5Ocg8kftMzGQGDaDt/6AJsHOQbX/x7SL3tbBFVCVPPwGoMPeRL6cO1SL8ytr15O+i7GfVOTrhxCcJaIVt4z3qJQJBAMD0TTVtn/lAfJDShT+q5k4UItd0LuGaz8X7/J8ZfosD5+xIO6q404HhRtoo/pBRDwPL28mRDtiVfc9UiT+re2MCQQC8nv/jOZsGuxoo/z0fuBTS+4DkQLqqF/PV2e9ZxGCvpy9ldzMAFDYfDVBuGZzaE0VDq0NIIL9JC0v/oi11LUMLAkEAkxVnCXQwPsrmaWrggeDfP+Nfj8cTuYvOGAh20DyZ1pQ6pUWGniPXrqiv3rrjGdWAUY+DUcpOe9Cwn2u8V5l7KwJANb4T6Lk0G6HZ3U6EKvJ3NHomimKLDQXQGuGCmhAjAElhbsacyGv507UmcohnsZcGCDbW78ks13IZkFe0HgvsawJAJDWLzfMplfZeO87J7ak28SZTV1hSx1eY2gqGU476/R3cjzKUi1Nq4HbddKKZUwI3w5E9wk9dGDPnyZ1ihihYzg==";
	static String publicKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCOK0BZgG8QiCQl15eBGMWjuUyogE3+a3IXxsUE2LjM1Yu/PydU8i/ofsjFz53fWl+PPRNM9UHvRyqnrND7Z900yC1zShYgy1EYNNULE62xnJbBdfLmt0/D5+TI8mky8ed66yL5cbo8WuGMrqiKGrS6kneIqU/SNQ/fGHR/jRA2QQIDAQAB";
	
	static String aesPublicKey = "UIMTdmdVQHfm5ZFADUDDng==";
	static String end="44ab93c7096b8d490dd2cfa54e1f9bb4e550130b66e83f0a6ff43a6dc3b8bb8c6871d379e522a80167fd7570bc2c66e7f4b4a49c4d561c7a8a655bdd25267e91411c5d95d2fec86e522a26e70314943e3eed7f22e77f056ae1bcfda48ae92749";
	public static void main(String[] args) {
		LiveParamDto liveParamDto = new LiveParamDto( deviceNum, channelId, liveId);
		//终端上传数据-转json
		String liveParamDtoStr=JSON.toJSONString(liveParamDto);
		//rsa私钥签名，输出hex格式
//		String endata =RSAUtil.encryptByPrivateKey(liveParamDtoStr.getBytes(), privateKey,true);
		
		
		String endata = AESUtil.encrypt(liveParamDtoStr, aesPublicKey,true);
		
		//参数拼接
		String dataToReq="deviceNum="+deviceNum+"&appId="+appId+"&us="+us+"&t="+t+"&endata="+endata+"";
		
		String dataToSign ="deviceNum="+deviceNum+"&appId="+appId+"&secretId="+secretId+"&us="+us+"&t="+t+"&endata="+endata+"";
		//参数排序按照key的ascii码
		String sortedDataToSign = ParamUtil.sortUrlParm(dataToSign);
		System.out.println("sortedDataToSign:"+sortedDataToSign);
		//对所有参数进行签名，输出hex格式
		String sgin=RSAUtil.sign(sortedDataToSign.getBytes(), privateKey, true);
		System.out.println("sgin:"+sgin);
		//组装新的url 数据
		String url = new String(dataToReq)+"&sign="+sgin;
		
		long a=System.currentTimeMillis();
		boolean isv=false;
		
		for(int i=0;i<1;i++) {
			 isv=RSAUtil.verify(sortedDataToSign.getBytes(), publicKey,sgin,  true);
		}
		long b=System.currentTimeMillis();
		
		System.out.println("b-a: "+(b-a));
		
		System.out.println("verify:"+isv);
		System.out.println("url:"+url);
		
		System.out.println("------------------");
		
//		String rrr="appId=0001&deviceNum=FF_48B02D0782FA&endata=15d52f29b62d49bba309762b59c1c10c7b71172febf2588f66674c0f8c35d38f1475d6e9f90dae239c67aa4c7253cb01bfdd1c833e37f6f744c30b61e4d7e72f46ff671779045c69e780f33581db2a86&t=1611316661&us=GR1aCmeo0PKE8KqeVIXDlA";
//		String sgin2=RSAUtil.sign(rrr.getBytes(), privateKey, true);
//		boolean isv2=RSAUtil.verify(sgin2.getBytes(), publicKey,sgin2,  true);
//		System.out.println("sgin2:"+sgin2);
//		System.out.println("verify2:"+isv2);
	}

}
